<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img
          src="https://ww2.sinaimg.cn/mw690/005AyeY8gw1eramxsxv2nj30af0a10st.jpg"
          alt=""
        />
        <span>商品后台管理系统</span>
      </div>

      <el-button type="info" @click="open">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iscloo ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          unique-opened
          :collapse="iscloo"
          router
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#409eef"
          style="border: none"
          :collapse-transition="false"
          :default-active="currentMenu"
        >
          <el-submenu
            v-for="item in menuList"
            :index="item.id + ''"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item
              @click="saveNavState('/' + subitem.path + '')"
              :index="'/' + subitem.path + ''"
              v-for="subitem in item.children"
              :key="subitem.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                {{ subitem.authName }}
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      menuList: [],
      iconList: {
        125: "fa fa-user",
        103: "fa fa-lock",
        101: "fa fa-archive",
        102: "fa fa-credit-card",
        145: "fa fa-bar-chart",
      },
      iscloo: false,
      currentMenu: "",
    };
  },
  created() {
    this.getMenuList();
    this.currentMenu = window.sessionStorage.getItem("activePath");
  },
  methods: {
    toggleCollapse() {
      this.iscloo = !this.iscloo;
    },
    saveNavState(str) {
      window.sessionStorage.setItem("activePath", str);
      this.currentMenu = str;
    },
    open() {
      this.$confirm("是否确认退出, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.signout();
          this.$message({
            type: "success",
            message: "退出成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消退出",
          });
        });
    },
    signout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$axios.get(
        "http://timemeetyou.com:8889/api/private/v1/menus"
      );
      console.log(res);
      if (res.meta.status != 200) return this.$message(res.meta.msg);
      this.menuList = res.data;
    },
  },
};
</script>

<style scoped="">
.btn {
  display: flex;
  justify-content: flex-end;
}

.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-header div {
  display: flex;
  align-items: center;
  font-size: 25px;
}

.el-header div img {
  display: inline-block;
  height: 10%;
  width: 10%;
  border-radius: 50%;
  margin-right: 20px;
}

.el-aside {
  background-color: #333744;
  transition: 0.2s;
}

.el-main {
  padding: 0;
  margin: 0;
}

.el-icon-menu,
.fa {
  font-size: 16px;
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
